/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package slic.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import slic.model.Escuela;
import slic.service.EscuelaLocalServiceUtil;
import slic.service.base.EscuelaLocalServiceBaseImpl;
import slic.service.persistence.EscuelaUtil;

import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;


/**
 * The implementation of the Escuela local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link slic.service.EscuelaLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Pablo Carballo
 * @see slic.service.base.EscuelaLocalServiceBaseImpl
 * @see slic.service.EscuelaLocalServiceUtil
 */
public class EscuelaLocalServiceImpl extends EscuelaLocalServiceBaseImpl {
	
	private DynamicQuery createDynamicQueryFromMap(Map<String, String> map, boolean count) {

		DynamicQuery query = DynamicQueryFactoryUtil.forClass(Escuela.class);

		if (map.get("listEscuelaId") != null && !map.get("listEscuelaId").trim().equals("")) {
			Collection<Object> colEscuelaId = new LinkedList<Object>();
			for (String escuelaId : map.get("listEscuelaId").trim().split(",")) {
				colEscuelaId.add(Long.valueOf(escuelaId));
			}

			query.add(PropertyFactoryUtil.forName("escuelaId")
					.in(colEscuelaId));
		}

		if (map.get("escuelaId") != null && !map.get("escuelaId").trim().equals(""))
			query.add(PropertyFactoryUtil.forName("escuelaId")
					.eq(Long.valueOf(map.get("escuelaId").trim())));

		if (map.get("nombre") != null && !map.get("nombre").trim().equals(""))
			query.add(PropertyFactoryUtil.forName("nombre")
					.like("%" + map.get("nombre").trim() + "%"));

		if (map.get("emails") != null && !map.get("emails").trim().equals(""))
			query.add(PropertyFactoryUtil.forName("emails")
					.like("%" + map.get("emails").trim() + "%"));
		
		if (!count && map.get("orderBy") != null && !map.get("orderBy").trim().equals("")) {
			if (map.get("orderType") != null && map.get("orderType").trim().equals("desc"))
				query.addOrder(OrderFactoryUtil.desc(map.get("orderBy")));
			else
				query.addOrder(OrderFactoryUtil.asc(map.get("orderBy")));
		}

		return query;

	}

	public List<Escuela> findByMap(Map<String, String> map) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, false);

		return EscuelaUtil.findWithDynamicQuery(query);

	}

	public List<Escuela> findByMap(Map<String, String> map, int start, int end) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, false);

		return EscuelaUtil.findWithDynamicQuery(query, start, end);

	}

	public long countByMap(Map<String, String> map) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, true);

		return new EscuelaUtil().countWithDynamicQuery(query);

	}
	
	public List<String> getRegiones() throws SystemException {
		
		DynamicQuery q = DynamicQueryFactoryUtil.forClass(Escuela.class)
				.setProjection(
						ProjectionFactoryUtil.distinct(
								ProjectionFactoryUtil.property("region"))
				).addOrder(OrderFactoryUtil.asc("region"));	
		
		List<String> results = new ArrayList<String>();		
				
		for (Object region : EscuelaLocalServiceUtil.dynamicQuery(q)) {
			results.add((String) region);			
		}
		
		return results;
		
	}
	
	public List<Escuela> findByRegion(String region) throws SystemException {
		
		return EscuelaUtil.findByRegion(region);
		
	}

	
}
